﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace Helpers
{
    public static class SqlHelper
    {
        /// <summary>
        /// 查询操作
        /// </summary>
        /// <param name="sqlStr">sql语句</param>
        /// <returns></returns>
        public static DataTable ExecReader(string sqlStr, string connectStr)
        {
            DataSet dataSet = new DataSet();
            DataTable dataTable = new DataTable();
            
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlStr, connectStr);
                sqlDataAdapter.Fill(dataSet);
                dataTable = dataSet.Tables[0];
            
            return dataTable;
        }
        /// <summary>
        /// 增删改操作
        /// </summary>
        /// <param name="sqlStr">sql语句</param>
        /// <param name="ps">参数</param>
        /// <returns></returns>
        public static int ExecuteNoQuery(string sqlStr, string connectStr, params SqlParameter[] ps)
        {
            int row = -1;
            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(connectStr))
                {
                    SqlCommand sqlCommand = new SqlCommand(sqlStr, sqlConnection);
                    sqlConnection.Open();
                    sqlCommand.Parameters.AddRange(ps);
                    row = sqlCommand.ExecuteNonQuery();
                }
            }
            catch 
            {
                row = -1;
            }
            return row;
        }
    }
}
